package com.exchange.controller.app.user;

import com.exchange.constant.AppConst;
import com.exchange.constant.BizConst;
import com.exchange.interceptor.BaseValidator;
import com.exchange.interceptor.EnumError;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

public class UserValidator extends BaseValidator {
	
    protected void validate(Controller c) {
    	setRet(Ret.fail());
    	this.setShortCircuit(true);//短路型验证
    	
    	
        String url = getActionKey();
        if (url.equals("/app/user/sendRegisterMessage")) {
        	typeAndTo(c.getInt("type"));         
        }
        if (url.equals("/app/user/register")) {
        	typeAndTo(c.getInt("type"));
        	validateAlphanumericTotal("invitationCode", AppConst.DEFAULT_INVITATION_CODE_SIZE, AppConst.DEFAULT_INVITATION_CODE_SIZE, "msg", EnumError.invitation_NULL.getCode());
        	validateAlphanumericTotal("vcode", AppConst.DEFAULT_CODE_LENGTH, AppConst.DEFAULT_CODE_LENGTH, "msg", EnumError.SMS_VCode_INVALID.getCode());
        	validateAccount("account","msg", EnumError.ACCOUNT_FORMAT_ERROR.getCode());
        	validatePassword("password","msg", EnumError.PASSWORD_ERROR.getCode()); 
        }
        if (url.equals("/app/user/login")) {
        	validateAccount("account","msg", EnumError.ACCOUNT_FORMAT_ERROR.getCode());
        	validatePassword("password","msg", EnumError.PASSWORD_ERROR.getCode()); 
        }
        if (url.equals("/app/user/fCaptcha")) {
        	if(c.getInt("type") != BizConst.MOBILE && c.getInt("type") != BizConst.EMAIL) {
        		addError("msg", EnumError.SMS_VCode_Send_TYPE_ERROR.getCode());
    		}   
	    	validateAccount("account","msg", EnumError.ACCOUNT_FORMAT_ERROR.getCode());
        }
        if (url.equals("/app/user/forget")) {
        	validateAccount("account","msg", EnumError.ACCOUNT_FORMAT_ERROR.getCode());
        	validateAlphanumericTotal("vcode", AppConst.DEFAULT_CODE_LENGTH, AppConst.DEFAULT_CODE_LENGTH, "msg", EnumError.SMS_VCode_INVALID.getCode());
        	validatePassword("password","msg", EnumError.PASSWORD_ERROR.getCode()); 
        }
        if (url.equals("/app/user/mdfLoginPassword")) {
        	validatePassword("oldPassword","msg", EnumError.PASSWORD_ERROR.getCode()); 
        	validatePassword("newPassword","msg", EnumError.PASSWORD_ERROR.getCode());
        	if(c.get("oldPassword").equals(c.get("newPassword"))) {
        		addError("msg", EnumError.new_old_eq.getCode());
        	}
        }
        if (url.equals("/app/user/mdfTradePassword")) {
        	validateAlphanumericTotal("vcode", AppConst.DEFAULT_CODE_LENGTH, AppConst.DEFAULT_CODE_LENGTH, "msg", EnumError.SMS_VCode_INVALID.getCode());
        	validateAlphanumericTotal("newTradePassword", 100, 6, "msg", EnumError.SMS_VCode_INVALID.getCode());
        }
        if (url.equals("/app/user/authorization")) {
        	validateAlphanumericTotal("cipher", 100, 6, "msg", EnumError.SMS_VCode_INVALID.getCode());
        }
        if (url.equals("/app/user/bCaptcha")) {
        	typeAndTo(c.getInt("type"));
        }
        if (url.equals("/app/user/bind")) {
        	typeAndTo(c.getInt("type"));
        	validateAlphanumericTotal("vcode", AppConst.DEFAULT_CODE_LENGTH, AppConst.DEFAULT_CODE_LENGTH, "msg", EnumError.SMS_VCode_INVALID.getCode());        	
        }
        if (url.equals("/app/user/addInvitationCode")) {
        	validateAlphanumericTotal("invitationCode",AppConst.DEFAULT_INVITATION_CODE_SIZE, AppConst.DEFAULT_INVITATION_CODE_SIZE, "msg", EnumError.invitation_NULL.getCode());        	
        }
        if (url.equals("/app/user/upload")) {
        	long siz = c.getFile().getFile().length();
        	if(siz > 1048576L) {//头像小于1mb
        		addError("msg", EnumError.img_too_big.getCode());
        	}
        }
    }
    
    //type,to验证
    protected void typeAndTo(int type) {
    	if(type == BizConst.MOBILE) {
    		validatePhone("to", "msg", EnumError.PHONE_FORMAT_ERROR.getCode());
    	}else if(type == BizConst.EMAIL) {
    		validateEmail("to", "msg", EnumError.PHONE_FORMAT_ERROR.getCode());
		}else {
			addError("msg", EnumError.SMS_VCode_Send_TYPE_ERROR.getCode());
		}   
    }

}
